package com.weiter.system.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiter.common.core.core.domain.entity.SysOperLog;
import com.weiter.common.core.core.page.PageQuery;
import com.weiter.common.core.service.SysOperLogApiService;
import com.weiter.system.mapper.SysOperLogMapper;
import com.weiter.system.service.ISysOperLogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 操作日志
 * @author weiter
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService, SysOperLogApiService {

    @Resource
    private SysOperLogMapper operLogMapper;

    /**
     * 查询操作日志分页列表
     * @param query     操作日志查询条件
     * @param pageQuery 分页查询信息
     * @return 操作日志列表
     */
    @Override
    public Page<SysOperLog> selectOperLogPage(SysOperLog query, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOperLog> lqw = buildQueryWrapper(query);
        return baseMapper.selectPage(pageQuery.build(), lqw);
    }

    /**
     * 查询操作日志列表
     * @param query 操作日志查询条件
     * @return 操作日志列表
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog query) {
        LambdaQueryWrapper<SysOperLog> lqw = buildQueryWrapper(query);
        return baseMapper.selectList(lqw);
    }

    /**
     * 新增操作日志
     * @param operLog 操作日志信息
     * @return 结果
     */
    @Override
    public int insertOperLogForApi(SysOperLog operLog) {
        return operLogMapper.insert(operLog);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        operLogMapper.cleanOperLog();
    }

    private LambdaQueryWrapper<SysOperLog> buildQueryWrapper(SysOperLog query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<SysOperLog> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(query.getOperIp()), SysOperLog::getOperIp, query.getOperIp());
        lqw.like(StrUtil.isNotBlank(query.getTitle()), SysOperLog::getTitle, query.getTitle());
        lqw.eq(ObjUtil.isNotNull(query.getBusinessType()), SysOperLog::getBusinessType, query.getBusinessType());
        lqw.in(ArrayUtil.isNotEmpty(query.getBusinessTypes()), SysOperLog::getBusinessTypes, Arrays.asList(query.getBusinessTypes()));
        lqw.like(StrUtil.isNotBlank(query.getOperName()), SysOperLog::getOperName, query.getOperName());
        lqw.eq(ObjUtil.isNotNull(query.getStatus()), SysOperLog::getStatus, query.getStatus());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
                SysOperLog::getOperTime, params.get("beginTime"), params.get("endTime"));
        lqw.orderByDesc(SysOperLog::getOperTime);
        return lqw;
    }

}
